﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace FamilyCompare
{
	internal class ChildResultPackerDataStructure
	{
		internal int CountSum
		{
			get
			{
				return this.countSum;
			}
		}

		internal int CountId
		{
			get
			{
				return this.countId;
			}
		}

		internal Dictionary<int, List<ResultPackerBase>> ThisValue
		{
			get
			{
				return this.thisValue;
			}
		}

		internal ChildResultPackerDataStructure()
		{
			this.thisValue = new Dictionary<int, List<ResultPackerBase>>();
		}

		internal void Clear()
		{
			this.thisValue.Clear();
			this.countSum = 0;
			this.countId = 0;
		}

		internal void AddPacker(ResultPackerBase input)
		{
			int id = input.ID;
			if (!this.thisValue.Keys.Contains(id))
			{
				this.thisValue.Add(id, new List<ResultPackerBase>());
				this.countId++;
			}
			this.thisValue[id].Add(input);
			this.countSum++;
		}

		internal void Order()
		{
			int count = this.thisValue.Keys.Count;
			Dictionary<int, List<ResultPackerBase>>.KeyCollection keys = this.thisValue.Keys;
			for (int i = 0; i < count; i++)
			{
				int key = keys.ElementAt(i);
				this.thisValue[key] = (from k in this.thisValue[key]
				orderby k.Name
				select k).ToList<ResultPackerBase>();
			}
			this.thisValue = (from k in this.thisValue
			orderby k.Key
			select k).ToDictionary((KeyValuePair<int, List<ResultPackerBase>> t) => t.Key, (KeyValuePair<int, List<ResultPackerBase>> t) => t.Value);
		}

		internal bool IfHave(ResultPackerBase input)
		{
			int id = input.ID;
			if (!this.thisValue.Keys.Contains(id))
			{
				return false;
			}
			foreach (ResultPackerBase resultPackerBase in this.thisValue[id])
			{
				if (resultPackerBase.Equals(input))
				{
					return true;
				}
			}
			return false;
		}

		internal ResultPackerBase GetResultPacker(ResultPackerBase input)
		{
			ResultPackerBase result = null;
			int id = input.ID;
			if (this.thisValue.Keys.Contains(id))
			{
				foreach (ResultPackerBase resultPackerBase in this.thisValue[id])
				{
					if (resultPackerBase.Equals(input))
					{
						result = resultPackerBase;
					}
				}
			}
			return result;
		}

		private Dictionary<int, List<ResultPackerBase>> thisValue;

		private int countSum;

		private int countId;
	}
}
